
from util import check_condition
import json
import os


# 检查条件1 --- mmi.name 在 ECP_C0_VB.c 中是否有 CanAppSend_XXX 或 IlPutTx_XXX
def check_condition_1(ecp_file, mmi_name):
    patterns = [f"CanAppSend_{mmi_name}", f"IlPutTx_{mmi_name}"]
    return check_condition(ecp_file, patterns, "条件1", mmi_name)

# 检查条件2 --- 检查条件2：no_mmi.name 在 CanApp_Rcv.c 中是否存在
def check_condition_2(rcv_file, no_mmi_name):
    return check_condition(rcv_file, no_mmi_name, "条件2", no_mmi_name)

# 执行所有检查
def isExist(signal_data, base_path):

    data = json.loads(signal_data)
    mmi_name = data["mmi"]["name"]
    no_mmi_name = data["no_mmi"]["name"]
    
    ecp_file = os.path.join(base_path, "ECP", "ECP_C0_VB.c")
    rcv_file = os.path.join(base_path, "CAN", "CANAPP", "CanApp_Rcv.c")
    
    result_1_status, result_1_msg = check_condition_1(ecp_file, mmi_name)
    result_2_status, result_2_msg = check_condition_2(rcv_file, no_mmi_name)
    
    # 输出结果
    if mmi_name is not None:
        print("条件1检查结果：" + result_1_msg)
    else:
        result_1_status = False

    if no_mmi_name is not None:
        print("\n条件2检查结果：" + result_2_msg)
    else:
        result_2_status = False
    
    # 综合判断
    all_passed = result_1_status or result_2_status
    print("\n" + ("所有条件均满足。" if all_passed else "存在不满足的条件。"))

    return all_passed
